package city.spring.modules.medicine.service.impl;

import city.spring.modules.medicine.entity.MedicineinventoryMedicineExtEntity;
import city.spring.modules.medicine.repository.MedicineinventoryMedicineExtRepository;
import city.spring.modules.medicine.service.MedicineinventoryMedicineExtService;
import city.spring.modules.medicine.vo.MedicineinventoryMedicineVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service：药品盘点计划与药品关系表
 *
 * @author 谢金婷
 * @date 2020-07-27 09:44:32
 */
@CacheConfig(cacheNames = {MedicineinventoryMedicineExtServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class MedicineinventoryMedicineExtServiceImpl extends ServiceImpl<MedicineinventoryMedicineExtRepository, MedicineinventoryMedicineExtEntity> implements MedicineinventoryMedicineExtService {

    private final MedicineinventoryMedicineExtRepository medicineinventoryMedicineExtRepository;

    public MedicineinventoryMedicineExtServiceImpl(MedicineinventoryMedicineExtRepository medicineinventoryMedicineExtRepository) {
        this.medicineinventoryMedicineExtRepository = medicineinventoryMedicineExtRepository;
    }

    @Override
    public void saveMedicineinventoryMedicineExt(MedicineinventoryMedicineExtEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateMedicineinventoryMedicineExt(MedicineinventoryMedicineExtEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteMedicineinventoryMedicineExt(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteMedicineinventoryMedicineExt(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<MedicineinventoryMedicineExtEntity> getInventoryWarehousingAndDelivery(Pageable pageable) {
        IPage<MedicineinventoryMedicineExtEntity> page = MyBatisUtils.toPage(pageable);
        return medicineinventoryMedicineExtRepository.getInventoryWarehousingAndDelivery(page, lambdaQuery().getWrapper());
    }

    @Override
    public IPage<MedicineinventoryMedicineVO> listPage(Page<? extends MedicineinventoryMedicineExtEntity> page, Wrapper<? extends MedicineinventoryMedicineExtEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }

}